#!/usr/bin/env bash
set -o pipefail

# submodules we do not want to put in the wit-manifest.json
ignored_submodules='doc\/html'

function check_submodule {
  jq -e ".[]|select(.commit == \"$1\")" wit-manifest.json || \
    (>&2 echo "$2 does NOT have a matching wit-manifest.json dependency!"; exit 1)
}

export -f check_submodule

# only check that all submodules have a corresponding wit package
# the wit-manifest.json may have packages that are not submoduled
# e.g. api-chisel3-sifive
git submodule status \
  | sed -r "s/[ -+U]([a-zA-Z0-9]+) ([^ ]+) ?.*/\1 \2/g" \
  | awk "!/$ignored_submodules/" \
  | xargs -d '\n' -I % bash -c "check_submodule %"
